/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Controller;

import Cart.Cart;
import Customer.CustomerManager;
import Entities.Item;
import Entities.Customer;
import DAO.DAO;
import Exception.KwickKioskDBException;
import GUI.CartScreen;
import GUI.ItemDescription;
import GUI.LogIn;
import GUI.POS_SearchByCategory;
import GUI.POS_SearchByUPC;
import GUI.POS_SearchResults;
import GUI.POS_Welcome;
import GUI.POS_SearchChoice;
import GUI.mainFrame;
import java.awt.*;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;

/**
 *
 * @author satkins
 */
public class POSController {

    private mainFrame frame;
    private Container pane;
    private POSController posControl;
    private boolean loggedIn = false;
    private Cart cart;
    private CustomerManager customerManager;
    private DAO dao;

    public POSController(){
        try {
            dao = new DAO();
        } catch (KwickKioskDBException ex) {
            //NEED TO IMPLEMENT ERROR HANDLING
            Logger.getLogger(POSController.class.getName()).log(Level.SEVERE, null, ex);
        }
        customerManager = new CustomerManager(dao);
        cart = new Cart(dao);
    }

    public void build(){
        frame = new mainFrame(this);

        // get the content pane and set layout to null
        pane = frame.getContentPane();


        POSWelcomeScreen();

        // set conditions of the frame      
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        frame.setLocation(((int)screenSize.getWidth()/2)-frame.getWidth()/2,
                ((int)screenSize.getHeight()/2)-frame.getHeight()/2 );
        frame.setVisible(true);
    }

    /* GETTERS AND SETTERS ****************************************************/

    public void setLoggedIn(boolean val){
        loggedIn = val;
        frame.Login(getFirstName());
    }

    public ArrayList<Item> getSearchResults(){
        return cart.getSearchResults();
    }

    public boolean isLoggedIn() {
        return loggedIn;
    }

    public String getFirstName(){
        return customerManager.getCustomer().getFirstName();
    }

    public String getNumItemsInCart(){
        return Integer.toString(cart.getItemsInCart().size());
    }


    /* FUNCTIONALITY **********************************************************/

    public void searchByUPC(String word){
        cart.searchByUPC(word);
        SearchResultsScreen();
    }

    public boolean addStoreCard(String storeCard){
        if(customerManager.setCustomer(storeCard))
            return true;
        else
            return false;
    }

    public void Logout(){
        customerManager.setCustomer(new Customer());
    }

    public void addItemToCart(Item item){
        cart.addItem(item);
    }



    /** SCREENS ***************************************************************/

    public void POSWelcomeScreen() {
        POS_Welcome welcome = new POS_Welcome(this);
        welcome.setBounds(0,0, 650, 500);
        frame.changeScreen(welcome);
        pane.validate();
        pane.repaint();

    }

    public void POSSearchUPCScreen() {
        POS_SearchByUPC search = new POS_SearchByUPC(this);
        search.setBounds(0,0, 650, 500);
        frame.changeScreen(search);
        pane.validate();
        pane.repaint();
    }

    public void AddStoreCardScreen(){
        LogIn login = new LogIn(this);
        login.setBounds(0,0, 650, 500);
        frame.changeTitle("Add Store Card");
        frame.changeScreen(login);
        pane.validate();
        pane.repaint();
    }

    public void SearchResultsScreen(){
        POS_SearchResults resultsScreen = new POS_SearchResults(this);
        resultsScreen.setBounds(0,0, 650, 500);
        frame.changeTitle("Search");
        frame.changeScreen(resultsScreen);
        pane.validate();
        pane.repaint();
    }

    public void ItemDescriptionScreen(Item selection){
        ItemDescription itemDescription = new ItemDescription(this, selection);
        itemDescription.setBounds(0,0, 650, 500);
        frame.changeTitle("Item Info");
        frame.changeScreen(itemDescription);
        pane.validate();
        pane.repaint();
    }

    public void SearchChoiceScreen(){
        POS_SearchChoice choice = new POS_SearchChoice(this);
        choice.setBounds(0,0, 650, 500);
        frame.changeTitle("Search");
        frame.changeScreen(choice);
        pane.validate();
        pane.repaint();
    }

    public void SearchCategoryScreen(){
        POS_SearchByCategory categoryScreen = new POS_SearchByCategory(this);
        categoryScreen.setBounds(0,0, 650, 500);
        frame.changeTitle("Search");
        frame.changeScreen(categoryScreen);
        pane.validate();
        pane.repaint();
    }

    public void CartScreen(){
        CartScreen cartScreen = new CartScreen(this, cart);
        cartScreen.setBounds(0,0, 650, 500);
        frame.changeTitle("Cart");
        frame.changeScreen(cartScreen);
        pane.validate();
        pane.repaint();
    }

}
